/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TCP.java
 *
 * Created on Apr 16, 2009, 4:34:04 PM
 */

package sim;

import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *
 * @author Quest
 */
public class TCP extends javax.swing.JFrame {

    /** Creates new form TCP */
    public TCP() {
        initComponents();
        this.setTitle("RB - Send Message");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btSend = new javax.swing.JButton();
        txtText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Enter your text:");

        btSend.setText("Send");
        btSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSendActionPerformed(evt);
            }
        });

        txtText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTextKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtText, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(btSend)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addComponent(btSend)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSendActionPerformed
        // TODO add your handling code here:
          String sentence;
          String modifiedSentence;
          //BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
          try{
          Socket clientSocket = new Socket("localhost", 14188);
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          //sentence = inFromUser.readLine();
          sentence = txtText.getText();
          outToServer.writeBytes(sentence + '\n');
          modifiedSentence = inFromServer.readLine();
          //System.out.println("FROM SERVER: " + modifiedSentence);
          txtText.setText(modifiedSentence);
          clientSocket.close();
          } catch (Exception exc){
              txtText.setText(exc.getMessage());
          }
    }//GEN-LAST:event_btSendActionPerformed

    private void txtTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTextKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER){
            sendMes();
        }
    }//GEN-LAST:event_txtTextKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[])  throws Exception {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TCP().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtText;
    // End of variables declaration//GEN-END:variables

    private void sendMes(){
          String sentence;
          String modifiedSentence;
          //BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
          try{
          Socket clientSocket = new Socket("localhost", 14188);
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          //sentence = inFromUser.readLine();
          sentence = txtText.getText();
          outToServer.writeBytes(sentence + '\n');
          modifiedSentence = inFromServer.readLine();
          //System.out.println("FROM SERVER: " + modifiedSentence);
          txtText.setText(modifiedSentence);
          clientSocket.close();
          } catch (Exception exc){
              txtText.setText(exc.getMessage());
          }
    }
}
